
$(document).keydown(function (e) {
    if (e.keyCode == 13) {
        cpwd();
    }
});

function cpwd() {
    layui.config({
        base: "js/"
    }).use(['form', 'element', 'layer', 'jquery'], function () {
        var layer = parent.layer === undefined ? layui.layer : parent.layer, $ = layui.jquery;

        if(!checkCpwd()){
            return false;
        }
        var options = {
            dataType: "json",
            async: false,
            success: function (data) {
                if (data && data.code == 0) {
                    layer.msg(data.msg ? data.msg : "修改密码成功~", {icon: 6});
                    parent.location.reload(); //刷新
                } else {
                    layer.msg(data.msg ? data.msg : "修改密码失败~", {icon: 5});
                }

            },
            error: function (data) {
                layer.msg(data.msg ? data.msg : "修改密码失败~");
            },
            complete: function () {
                parent.location.reload(); //刷新
            }
        };
        $("#cpwdForm").ajaxSubmit(options);
    })
}

function checkCpwd() {
    var oldPwd = $("#oldPwd").val(), newPwd = $("#newPwd").val(), againPwd = $("#againPwd").val();
    if (!oldPwd) {
        layer.msg("请输入旧密码~", {icon: 5});
        return false;
    }
    if (!newPwd || !againPwd) {
        layer.msg("请输入新密码~", {icon: 5});
        return false;
    }
    if (newPwd.length < 6 || againPwd.length < 6) {
        layer.msg("新密码长度太短~", {icon: 5});
        return false;
    }
    if (newPwd != againPwd) {
        layer.msg("两次密码不匹配~", {icon: 5});
        return false;
    }
}
